package com.xlh.support.quiz.controller;

import com.xlh.common.Result;
import com.xlh.support.quiz.po.QuizCreatePO;
import com.xlh.support.quiz.service.QuizManageService;
import com.xlh.support.quiz.vo.QuizListVO;
import com.xlh.support.quiz.vo.QuizVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@Api("随堂提问管理")
@RestController
@RequestMapping("/quiz")
public class QuizManageController {

    @Autowired
    private QuizManageService quizManageService;

    @ApiOperation("查询提问")
    @GetMapping
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<QuizListVO> listQuiz(@RequestParam Long chapterId,
                                       @RequestParam Integer pageNumber) {
        List<QuizVO> quizVOs = quizManageService.listQuiz(chapterId, pageNumber);
        QuizListVO quizListVO = QuizListVO.builder()
                .size(quizVOs.size())
                .quizList(quizVOs)
                .build();
        return Result.success(quizListVO);
    }

    @ApiOperation("创建提问")
    @PostMapping
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result createQuiz(@RequestBody @Valid QuizCreatePO quizCreatePO) {
        quizManageService.createQuiz(quizCreatePO);
        return Result.success();
    }

    @ApiOperation("删除提问")
    @DeleteMapping("/{quizId}")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result deleteQuiz(@PathVariable("quizId") Long quizId) {
        quizManageService.deleteQuiz(quizId);
        return Result.success();
    }
}
